package eshop.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import eshop.entity.Categories;
import eshop.entity.Products;

@WebServlet("/dothing.do")
public class ViewCategory extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		int page = 1;
		//Lấy số page từ jsp
		if(req.getParameter("page")!=null){
			page = Integer.parseInt(req.getParameter("page"));
		}
		
		int noOfPages ;	 // số page
		int recordPerPage = 5;//số bản ghi trên page
		
		int firstRecord = (page*5)-5;

		
		
		Configuration config = new AnnotationConfiguration().configure();
		SessionFactory factory = config.buildSessionFactory();
		Session session = factory.openSession();
		Transaction t = session.beginTransaction();
		
		String hql="SELECT count(Id) FROM Products";
		Query query = session.createQuery(hql);
		long countProduct =(long)query.uniqueResult();// Lấy tổng số lượng sản phẩm
		//System.out.println(count); // Duyệt số lượng bảng theo tên sản phẩm
		
		//Tính tổng số page
		noOfPages = (int)countProduct/recordPerPage;
		
		//Lấy 5 bản ghi trên page
		hql = "FROM Products";
		query = session.createQuery(hql);
		query.setFirstResult(firstRecord);
		query.setMaxResults(recordPerPage);
		List<Products> plist = query.list();

		
		
		//Chức năng:
		
		//String edit = req.getParameter("edit"); câu lệnh này gây lỗi vì lần chạy
		// đầu tiên chưa xuất hiện getParameter 
		
		try{
		
		if(req.getParameter("edit")!=null){
			int edit =Integer.parseInt(req.getParameter("edit"));
			Products pId = (Products)session.load(Products.class,edit);
			
			req.setAttribute("nameproduct",pId.getName());
			req.setAttribute("idproduct",pId.getId());
			
			req.setAttribute("idcategory",pId.getCate().getId());
			req.setAttribute("name",pId.getCate().getName());
			req.setAttribute("namevn",pId.getCate().getNameVN());
			
			req.setAttribute("unitprice",pId.getUnitPrice());
			req.setAttribute("image",pId.getImage());
			req.setAttribute("productdate",pId.getProductDate());
			req.setAttribute("available",pId.getAvailable());
			req.setAttribute("quantity",pId.getQuantity());
		}
		
		
		else if(req.getParameter("btnInsert")!=null){
			String nameproduct = req.getParameter("nameproduct");
			Integer idproduct = Integer.parseInt(req.getParameter("idproduct"));
			Integer idcategory =Integer.parseInt(req.getParameter("idcategory"));
			String name = req.getParameter("name");
			String namevn = req.getParameter("namevn");
			Double unitprice =Double.parseDouble(req.getParameter("unitprice"));
			String image = req.getParameter("image");
			Date productdate = java.sql.Date.valueOf(req.getParameter("productdate"));
			Boolean available = Boolean.parseBoolean(req.getParameter("available"));		
			Integer quantity = Integer.parseInt(req.getParameter("quantity"));
			
			Products pSave = new Products();
			
			pSave.setName(nameproduct);
			pSave.setId(idproduct);
			
			pSave.getCate().setId(idcategory);
			pSave.getCate().setName(name);
			pSave.getCate().setNameVN(namevn);
			
			pSave.setUnitPrice(unitprice);
			pSave.setImage(image);
			pSave.setProductDate(productdate);
			pSave.setAvailable(available);
			pSave.setQuantity(quantity);
			
			session.save(pSave);
			
			t.commit();
			session.close();
			
			
		}
//		
		
		else if(req.getParameter("btnUpdate")!=null){
			
		}
	
		
		else if(req.getParameter("btnDelete")!=null){
			
		}
		}
		catch(Exception ex){
			t.rollback();
		}

		// Nếu edit thì past dữ liệu lên trên ô nhập
//		if(edit!=null){
//			Products pId = (Products)session.load(Products.class,edit);
//			req.setAttribute("nameproduct",pId.getName());
//		}
		
		
		
		//Gửi dữ liệu qua jsp
		req.setAttribute("currentPage", page);
		req.setAttribute("noOfPages",noOfPages);
		req.setAttribute("plist",plist);
		req.getRequestDispatcher("ViewCategory.jsp").forward(req, res);
		
	}
}
